from operator import attrgetter


class Solution:
    def bestTeamScore(self, scores, ages):
        scores_sorted = [b for a, b in sorted(zip(ages, scores))]
        scores_sorted = [0] + scores_sorted
        dp = [0 for _ in range(len(scores_sorted))]
        ans = 0
        for i in range(1, len(scores_sorted)):

            for j in range(i):
                if scores_sorted[i] >= scores_sorted[j]:
                    dp[i] = max(dp[i], dp[j] + scores_sorted[i])

            ans = max(ans, dp[i])

        return ans

    def bestTeamScores(self, scores, ages):
        # l = [(a, b) for a, b in sorted(zip(ages, scores))]
        l = [b for a, b in sorted(zip(ages, scores))]
        dp = [0 for _ in range(len(l))]
        ans = 0
        for i in range(len(l)):
            dp[i] = l[i]
            for j in range(i):
                if l[i] >= l[j]:
                    dp[i] = max(dp[i], dp[j] + l[i])
            ans = max(dp[i], ans)
        return ans


if __name__ == '__main__':
    scores = [1, 4, 5, 7, 3, 3]
    ages = [1, 2, 3, 5, 4, 2]
    s = Solution()
    print(s.bestTeamScore(scores, ages))
